<?php

error_reporting(E_STRICT | E_ALL);
date_default_timezone_set('America/New_York');
ini_set('display_errors', 1);
set_time_limit(60*60);
ini_set('memory_limit', '128M');

$g_start = microtime(true);
require_once "config.php";
require_once dirname(__FILE__). '/util/connection.class.php';
$dbconn = Connection::getInstance();

$toSubUsersId = getToSentUsers($dbconn);
if(count($toSubUsersId)>0){
	$usersInfo = getUsersInfo($toSubUsersId, $dbconn);
	everSaveSent($usersInfo, $dbconn);
}

echo "\n". (microtime(true)-$g_start) . " seconds\n";



function getToSentUsers(Connection $db)
{
	$sql = "SELECT uid FROM eversave_optin WHERE status='tosent' limit 50";
	try{
		$result = $db->query($sql);
		if(!empty($result)){
			foreach($result as $k=>$v){
				$result[$k] = $v['uid'];
			}	
		}
		return $result;
	}catch(Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

function getUsersInfo($usersId, Connection $db)
{
	$sql = " SELECT * FROM users WHERE id IN ( " . implode(', ', $usersId) . " ) ";
	try{
		$result = $db->query($sql);
		return $result;
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

function everSaveSent($usersInfo, Connection $db)
{
	foreach($usersInfo as $user){
		list($y, $m, $d) = explode('-', $user['birthday']);
		if ($user['site_id'] == 1)
		{
			$sourceid = 107951;
		}
		else if ($user['site_id'] == 2)
		{
			$sourceid = 108067;
		}
		else if ($user['site_id'] == 3)
		{
			$sourceid = 108068;
		}
		$urlArgs = array('emailaddress'=>$user['email'], 'sourceid'=>$sourceid, 'ipaddress'=>long2ip($user['ip']), 'fname'=>$user['firstname'], 
						 'lname'=>$user['lastname'], 'dobmonth'=>intval($m), 'dobday'=>$d, 'dobyear'=>$y, 'gender'=>$user['gender'], 
						 'addr1'=>$user['address1'], 'addr2'=>$user['address2'], 'city'=>$user['city'], 'state'=>$user['state'], 'zipcode'=>$user['zipcode']
		);
				
		$ch = curl_init();
		$url = "http://www.eversave.com/service/servlet/consumers.CoReg?";
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($urlArgs, '', '&'));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		$content = curl_exec($ch);
//		print_r(http_build_query($urlArgs, '', '&'));
//		echo "\n";
//		print_r($content);
		curl_close($ch);
		
		setStatusSub( $user['id'] , $content, $db);
	}
}

function setStatusSub($userId, $response, Connection $db)
{
	$status = 'sent_failed';
	if(strpos(strtolower($response), '1')){
		$status = 'sent';
	}
	
	$sql = " UPDATE eversave_optin SET status=?, send_date=?, response=? WHERE uid=? ";
	echo '' . $userId. "\n";
	try{
		$result = $db->execute($sql, array( $status , date('Y-m-d'), $response, $userId));
		return $result;
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

	
	
	
	
	
	
	